from multiprocessing import Process
from multiprocessing import Queue
import time
import random


# def producer(q):
#     time.sleep(3)
#     q.put('包子')
#     # for i in range(5):
#
#
#
#
# def consumer(q):
#     print(q.get())


def producer(name,q):
    for i in range(1,6):
        time.sleep(random.randint(1,3))
        res = f'{i}号包子'
        q.put(res)

        print(f'\033[0;32m 生产者{name}: 生产了{res}\033[0m')



def consumer(name,q):
    while 1:
        try:
            time.sleep(random.randint(1,3))
            ret = q.get(timeout=5)
            print(f'消费者{name}: 吃了{ret}')
        except Exception:
            return




if __name__ == '__main__':

    q = Queue()

    p1 = Process(target=producer, args=('太白',q))
    p2 = Process(target=consumer, args=('MC骚强',q))

    p1.start()
    p2.start()